<?php
/**
 * Created by PhpStorm.
 * User: Gustav
 * Date: 2018/8/28
 * Time: 20:10
 */

namespace app\user\controller;

use app\common\controller\GlobalVariable;
use app\common\controller\UserController;
use app\common\service\Validate;
use app\user\model\User;
use think\Request;
use think\Db;
use think\Session;

class Search extends UserController
{
    /**
     * 搜索总接收（1-整体搜索 2-收藏搜索 3-历史搜索）
     */

    public function resBegin()
    {
        $id = input("get.id");
        if ($id == 1) {
            $this->resAll();
        } elseif ($id == 2) {
            $this->resCollect();
        } elseif ($id == 3) {
            $this->resSeaHistory();
        } elseif ($id == 4) {
            $this->resHistory();
        }
    }

    /**
     * 整体搜索
     */

    public function resAll()
    {
        $token = Request::instance()->header('token');
        $find = Db::table('xwd_user_login_info')->where('token', $token)->find();
        $user_id = json_encode($find['user_id']);
        $mes = input("get.keywords");
        $num = input('get.num');
        $where['keywords'] = array('like', '%' . $mes . '%');
        $select = Db::table('xwd_doc_thesis')->where($where)->select();
        $long = count($select, 0);
        $ifS = Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 1])->find();
        if ($ifS) {
            $this->resReturnAll($user_id, $mes, $long, $num);
        } else {
            $thesis_list = [];
            for ($i = 0; $i < $long; $i++) {
                $thesis_list[$i] = $select[$i]["thesis_id"];
            }
            Db::table('xwd_user_search')->insert(['user_id' => $user_id, 'thesis_list' => json_encode($thesis_list), 'keywords' => $mes, 'time' => time(), 'length' => $long, 'type' => 1]);
            $this->resReturnAll($user_id, $mes, $long, $num);
        }

    }

    /**
     * 返回工作
     */

    public function resReturnAll($user_id, $mes, $long, $num)
    {
        if ($num == null) {
            $num = 5;
        }
        $find = Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 1])->find();
        $i = $find['num'];
        $thesis_list = json_decode($find["thesis_list"], true);
        $thesis = [];
        $last = $long - $i;
        if ($last >= $num) {
            $long_num = $i + $num;
        } elseif ($last < $num) {
            $long_num = $long;
        }
        for (; $i < $long_num; $i++) {
            $thesis_id = $thesis_list[$i];
            $a = Db::table('xwd_doc_thesis')->where('thesis_id', $thesis_id)->find();
            $journal_id = $a["journal_id"];
            $list2 = Db::table('xwd_doc_journal')->where('journal_id', $journal_id)->find();
            $a["periodical"] = $list2["periodical"];
            array_push($thesis, $a);
        }
        if ($i == $long) {
            Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 1])->update(['num' => 0, 'down' => 1]);
        } else {
            Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 1])->update(['num' => $i]);
        }
        return GlobalVariable::promptData($thesis);
    }

    /**
     * 收藏搜索
     */

    public function resCollect()
    {
        $token = Request::instance()->header('token');
        $find = Db::table('xwd_user_login_info')->where('token', $token)->find();
        $user_id = json_encode($find['user_id']);
        $mes = input("get.keywords");
        $num = input('get.num');
        $ifS = Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 2])->find();
        $find1 = Db::table('xwd_user_behavior')->where(['user_id' => $user_id, 'action_type' => 4])->select();
        $long = count($find1, 0);
        if ($ifS) {
            $this->resReturnCollect($user_id, $mes, $ifS['length'], $num);
        } else {
            $re_thesis_list = [];
            for ($i = 0; $i < $long; $i++) {
                $re_thesis_list[$i] = $find1[$i]['thesis_id'];
            }
            $thesis_list = [];
            for ($j = 0, $i = 0; $i < $long; $i++) {
                $thesis_id = $re_thesis_list[$i];
                $a = Db::table('xwd_doc_thesis')->where('thesis_id', $thesis_id)->find();
                $ifThis = stristr($a['keywords'], $mes);
                if ($ifThis == "") {
                    continue;
                }
                $thesis_list[$j++] = $thesis_id;
            }
            Db::table('xwd_user_search')->insert(['user_id' => $user_id, 'thesis_list' => json_encode($thesis_list), 'keywords' => $mes, 'time' => time(), 'length' => $j, 'type' => 2]);
            $this->resReturnCollect($user_id, $mes, $j, $num);
        }
    }

    /**
     * 返回工作
     */

    public function resReturnCollect($user_id, $mes, $long, $num)
    {
//        var_dump($long);
        if ($num == null) {
            $num = 5;
        }
        $find = Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 2])->find();
        $i = $find['num'];
        $thesis_list = json_decode($find["thesis_list"], true);
        $thesis = [];
        $last = $long - $i;
        if ($last >= $num) {
            $long_num = $i + $num;
        } elseif ($last < $num) {
            $long_num = $long;
        }
        for (; $i < $long_num; $i++) {
            $thesis_id = $thesis_list[$i];
            $a = Db::table('xwd_doc_thesis')->where('thesis_id', $thesis_id)->find();
            $journal_id = $a["journal_id"];
            $list2 = Db::table('xwd_doc_journal')->where('journal_id', $journal_id)->find();
            $a["periodical"] = $list2["periodical"];
            array_push($thesis, $a);
        }
        if ($i == $long) {
            Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 2])->update(['num' => 0, 'down' => 1]);
        } else {
            Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 2])->update(['num' => $i]);
        }
        return GlobalVariable::promptData($thesis);
    }

    /**
     * 历史搜索
     */

    public function resSeaHistory()
    {
        $token = Request::instance()->header('token');
        $find = Db::table('xwd_user_login_info')->where('token', $token)->find();
        $user_id = json_encode($find['user_id']);
        $mes = input("get.keywords");
        $num = input('get.num');
        $ifS = Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 3])->find();
        $find1 = Db::table('xwd_user_behavior')->where(['user_id' => $user_id, 'action_type' => 2])->select();
        $long = count($find1, 0);
        if ($ifS) {
            $this->resReturnHistory($user_id, $mes, $ifS['length'], $num);
        } else {
            $re_thesis_list = [];
            for ($i = 0; $i < $long; $i++) {
                $re_thesis_list[$i] = $find1[$i]['thesis_id'];
            }
            $thesis_list = [];
            for ($j = 0, $i = 0; $i < $long; $i++) {
                $thesis_id = $re_thesis_list[$i];
                $a = Db::table('xwd_doc_thesis')->where('thesis_id', $thesis_id)->find();
                $ifThis = stristr($a['keywords'], $mes);
//                echo $ifThis;
//                $ifThis = stristr("Power io", "1111");
//                echo $ifThis;
                if ($ifThis == "") {
                    continue;
                }
                $thesis_list[$j++] = $thesis_id;
            }
            Db::table('xwd_user_search')->insert(['user_id' => $user_id, 'thesis_list' => json_encode($thesis_list), 'keywords' => $mes, 'time' => time(), 'length' => $j, 'type' => 3]);
            $this->resReturnHistory($user_id, $mes, $j, $num);
        }
    }

    /**
     * 返回工作
     */

    public function resReturnHistory($user_id, $mes, $long, $num)
    {
//        var_dump($long);
        if ($num == null) {
            $num = 5;
        }
        $find = Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 3])->find();
        $i = $find['num'];
        $thesis_list = json_decode($find["thesis_list"], true);
        $thesis = [];
        $last = $long - $i;
        if ($last >= $num) {
            $long_num = $i + $num;
        } elseif ($last < $num) {
            $long_num = $long;
        }
        for (; $i < $long_num; $i++) {
            $thesis_id = $thesis_list[$i];
            $a = Db::table('xwd_doc_thesis')->where('thesis_id', $thesis_id)->find();
            $journal_id = $a["journal_id"];
            $list2 = Db::table('xwd_doc_journal')->where('journal_id', $journal_id)->find();
            $a["periodical"] = $list2["periodical"];
            array_push($thesis, $a);
        }
        if ($i == $long) {
            Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 3])->update(['num' => 0, 'down' => 1]);
        } else {
            Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => 3])->update(['num' => $i]);
        }
        return GlobalVariable::promptData($thesis);
    }

    /**
     * 历史记录返回
     */

    public function resHistory()
    {
        $token = Request::instance()->header('token');
        $find = Db::table('xwd_user_login_info')->where('token', $token)->find();
        $user_id = json_encode($find['user_id']);
        $mes = input("get.keywords");
        $hisNum = input("get.hisNum");
        $ifS = Db::table('xwd_user_search')->where(['user_id' => $user_id, 'keywords' => $mes, 'type' => $hisNum])->find();
        if (!$ifS) {
            if ($hisNum == 1) {
                $this->resAll();
            } elseif ($hisNum == 2) {
                $this->resCollect();
            } elseif ($hisNum == 3) {
                $this->resSeaHistory();
            }
        } else {
            $num = $ifS['num'];
            $down = $ifS['down'];
            if ($down == 0) {
                $thesis_list = json_decode($ifS['thesis_list'], true);
                $thesis = [];
                for ($i = 0; $i < $num; $i++) {
                    $thesis_id = $thesis_list[$i];
                    $a = Db::table('xwd_doc_thesis')->where('thesis_id', $thesis_id)->find();
                    $journal_id = $a["journal_id"];
                    $list2 = Db::table('xwd_doc_journal')->where('journal_id', $journal_id)->find();
                    $a["periodical"] = $list2["periodical"];
                    array_push($thesis, $a);
                }
                return GlobalVariable::promptData($thesis);
            } elseif ($down == 1) {
                $num = $ifS['length'];
                $thesis_list = json_decode($ifS['thesis_list'], true);
                $thesis = [];
                for ($i = 0; $i < $num; $i++) {
                    $thesis_id = $thesis_list[$i];
                    $a = Db::table('xwd_doc_thesis')->where('thesis_id', $thesis_id)->find();
                    $journal_id = $a["journal_id"];
                    $list2 = Db::table('xwd_doc_journal')->where('journal_id', $journal_id)->find();
                    $a["periodical"] = $list2["periodical"];
                    array_push($thesis, $a);
                }
                return GlobalVariable::promptData($thesis);
            }
        }
    }
}